﻿using Admin.ApplicationLib.Services;
using Admin.CommonLib.Constants;
using Admin.CommonLib.Models.Permission;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swimj.AspNetCore.Mvc;

namespace Admin.WebApi.Controllers
{
    [Authorize]
    [ApiController]
    public class PermissionController : ControllerBase
    {
        private readonly IPermissionService _permissionService;

        public PermissionController(IPermissionService permissionService)
        {
            _permissionService = permissionService;
        }

        /// <summary>
        /// 查询权限列表信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Permission.QueryPermissions)]
        public async Task<IActionResult> Get([FromQuery] PermissionSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _permissionService.QueryPermissionsByPageAsync(model);
                return Ok(data);
            }
            else
            {
                var data = await _permissionService.QueryPermissionsAsync(model);
                return Ok(data);
            }
        }

        [HttpGet(ApiRoutes.Permission.GetPermission)]
        public async Task<IActionResult> Get(Guid id)
        {
            var data = await _permissionService.GetPermissionAsync(id);
            return Ok(data);
        }

        [HttpPost(ApiRoutes.Permission.CreatePermission)]
        public async Task<IActionResult> Post(PermissionEditRequestDto model)
        {
            var data = await _permissionService.CreatePermissionAsync(model);
            return Ok(data);
        }

        [HttpPut(ApiRoutes.Permission.UpdatePermission)]
        public async Task<IActionResult> Put(Guid id, PermissionEditRequestDto model)
        {
            var data = await _permissionService.UpdatePermissionAsync(id, model);
            return Ok(data);
        }

        [HttpDelete(ApiRoutes.Permission.DeletePermission)]
        public async Task<IActionResult> Delete(Guid id)
        {
            var data = await _permissionService.DeletePermissionAsync(id);
            return Ok(data);
        }

        [HttpGet(ApiRoutes.Permission.ExportPermission)]
        [Authorize(nameof(ApiRoutes.Permission.ExportPermission))]
        public async Task<IActionResult> Export()
        {
            var data = await _permissionService.ExportAsync();
            return new CustomFileResult(data.Item1, data.Item2);
        }

        /// <summary>
        /// 导出用户下载模板
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Permission.ExportTemplatePermission)]
        [Authorize(nameof(ApiRoutes.Permission.ImportPermission))]
        public async Task<IActionResult> ExportTemplate()
        {
            var result = await _permissionService.ExportTemplateAsync();
            return new CustomFileResult(result.Item1, result.Item2);
        }

        [HttpPost(ApiRoutes.Permission.ImportPermission)]
        public async Task<IActionResult> Import([FromQuery] int importType, IFormFile file)
        {
            var stream = file.OpenReadStream();

            byte[] bytes = new byte[stream.Length];
            var _ = await stream.ReadAsync(bytes, 0, bytes.Length);
            //设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);

            MemoryStream memoryStream = new MemoryStream(bytes);
            var result = await _permissionService.ImportAsync(importType, memoryStream);
            return Ok(result);
        }


        /// <summary>
        /// 查询权限树形列表信息
        /// </summary>
        /// <param name="treeType"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Permission.TreePermission)]
        public async Task<IActionResult> GetPermissionsByTree(int treeType)
        {
            var data = await _permissionService.QueryPermissionsByTreeAsync(treeType);
            return Ok(data);
        }

        /// <summary>
        /// 查询当前登录用户的权限树形列表信息
        /// </summary>
        /// <param name="treeType"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Permission.CurrentUserPermission)]
        public async Task<IActionResult> GetCurrentUserPermissions(int treeType)
        {
            var data = await _permissionService.GetCurrentUserPermissions(treeType);
            return Ok(data);
        }
    }
}